function UserView() {
	this.parent();
}

UserView.inherits(BaseView, {
	init : function() {

		this.createLoadUserNode(this.ctrl.model.users);
	},

	createLoadUserNode : function(arrUsers) {
		var key = "user";
		addActive(key);
		var rootNode = $id("loadUser");
		rootNode.innerHTML = "";
		if (arrUsers.length > 0) {
			var nodeTable = createTable();
			nodeTable.setAttribute("id", "tableStaff");

			var nodeThId = createNode("th");
			var idTh = createTextNode("Mã nhân viên");
			nodeThId.appendChild(idTh);
			var nodeThUName = createNode("th");
			var uNameTh = createTextNode("Tài khoản");
			nodeThUName.appendChild(uNameTh);
			var nodeThLName = createNode("th");
			var lNameTh = createTextNode("Họ");
			nodeThLName.appendChild(lNameTh);
			var nodeThFName = createNode("th");
			var fNameTh = createTextNode("Tên");
			nodeThFName.appendChild(fNameTh);
			var nodeThEmail = createNode("th");
			var emailTh = createTextNode("E-mail");
			nodeThEmail.appendChild(emailTh);
			var nodeThAdd = createNode("th");
			var addTh = createTextNode("Địa chỉ");
			nodeThAdd.appendChild(addTh);
			var nodeThPhone = createNode("th");
			var phoneTh = createTextNode("Điện thoại");
			nodeThPhone.appendChild(phoneTh);
			var nodeThRole = createNode("th");
			var roleTh = createTextNode("Role");
			nodeThRole.appendChild(roleTh);
			var nodeThBan = createNode("th");
			var banTh = createTextNode("Banned");
			nodeThBan.appendChild(banTh);
			var nodeThUpdate = createNode("th");
			var nodeThDelete = createNode("th");

			nodeTable.appendChild(nodeThId);
			nodeTable.appendChild(nodeThUName);
			nodeTable.appendChild(nodeThLName);
			nodeTable.appendChild(nodeThFName);
			nodeTable.appendChild(nodeThEmail);
			nodeTable.appendChild(nodeThAdd);
			nodeTable.appendChild(nodeThPhone);
			nodeTable.appendChild(nodeThRole);
			nodeTable.appendChild(nodeThBan);
			nodeTable.appendChild(nodeThUpdate);
			nodeTable.appendChild(nodeThDelete);

			var nodeTr = null;
			var user = null;
			var nodeId = null;
			var nodeUserName = null;
			var nodeLName = null;
			var nodeFName = null;
			var nodeEmail = null;
			var nodeAdd = null;
			var nodePhone = null;
			var nodeRole = null;
			var nodeBan = null;
			var nodeTdUpdate = null;
			var nodeDivUpdate = null;
			var nodeUpdate = null;
			var nodeTdDelete = null;
			var nodeDivDelete = null;
			var nodeDelete = null;

			var length = arrUsers.length;

			for (var i = 0; i < length; i++) {
				user = arrUsers[i];
				nodeTr = createNode("tr");
				nodeTr.setAttribute("id", "userIdInRow" + user.staffId);

				nodeId = createNode("td");
				var id = createTextNode(user.staffId);
				nodeId.appendChild(id);

				nodeUserName = createNode("td");
				var uname = createTextNode(user.staffUsername);
				nodeUserName.appendChild(uname);

				nodeLName = createNode("td");
				var lname = createTextNode(user.staffLName);
				nodeLName.appendChild(lname);

				nodeFName = createNode("td");
				var fname = createTextNode(user.staffFName);
				nodeFName.appendChild(fname);

				nodeEmail = createNode("td");
				var email = createTextNode(user.staffEmail);
				nodeEmail.appendChild(email);

				nodeAdd = createNode("td");
				var addr = createTextNode(user.staffAddress);
				nodeAdd.appendChild(addr);

				nodePhone = createNode("td");
				var phone = createTextNode(user.staffPhone);
				nodePhone.appendChild(phone);

				nodeRole = createNode("td");
				var role = createTextNode(user.staffRole);
				nodeRole.appendChild(role);

				nodeBan = createNode("td");
				var ban = createTextNode(user.staffIsBan);
				nodeBan.appendChild(ban);

				//create node update
				nodeTdUpdate = createNode("td");
				nodeDivUpdate = createDiv();
				nodeTdUpdate.addEventListener("click", function(event) {
					var currentRow = $id(event.currentTarget.parentNode.id);
					var cels = currentRow.getElementsByTagName("td");
					var userId = cels[0].innerText;
					var userName = cels[1].innerText;
					var userLName = cels[2].innerText;
					var userFName = cels[3].innerText;
					var userEmail = cels[4].innerText;
					var userAddr = cels[5].innerText;
					var userPhone = cels[6].innerText;
					var userRole = cels[7].innerText;
					var banned = cels[8].innerText;

					var model = new staffModel(userId, userName, userLName, userFName, userEmail, userAddr, userPhone, userRole, banned);

					pagesManager.setPage(Pages.updateUser, false, model);
				});
				nodeUpdate = createNode("img");
				nodeUpdate.setAttribute("src", Pages.params.img + "update.png");
				nodeUpdate.style.cursor = "pointer";
				nodeDivUpdate.appendChild(nodeUpdate);
				nodeTdUpdate.appendChild(nodeDivUpdate);

				//create node delete
				nodeTdDelete = createNode("td");
				nodeDivDelete = createDiv();
				nodeDelete = createNode("img");
				nodeDelete.setAttribute("src", Pages.params.img + "delete_icon.png");
				nodeDelete.style.cursor = "pointer";
				nodeTdDelete.addEventListener("click", function(event) {
					var row = $id(event.currentTarget.parentNode.id);
					var cels = row.getElementsByTagName("td");
					var uId = cels[0].innerText;
					var uName = cels[1].innerText;
					var r = confirm("Bạn có muốn xóa user có tài khoản " + uName + "?");

					if (r == true) {
						delStaff(uId);
					}
				});
				nodeDivDelete.appendChild(nodeDelete);
				nodeTdDelete.appendChild(nodeDivDelete);

				nodeTr.appendChild(nodeId);
				nodeTr.appendChild(nodeUserName);
				nodeTr.appendChild(nodeLName);
				nodeTr.appendChild(nodeFName);
				nodeTr.appendChild(nodeEmail);
				nodeTr.appendChild(nodeAdd);
				nodeTr.appendChild(nodePhone);
				nodeTr.appendChild(nodeRole);
				nodeTr.appendChild(nodeBan);
				nodeTr.appendChild(nodeTdUpdate);
				nodeTr.appendChild(nodeTdDelete);

				nodeTable.appendChild(nodeTr);
			}
			rootNode.appendChild(nodeTable);
			var divPaging = createDiv();
			divPaging.setAttribute("id", "pageStaff");
			divPaging.setAttribute("class", "paging");
			var script = createNode("script");
			script.setAttribute("type", "text/javascript");
			script.innerHTML = "var pager = new Pager('tableStaff', 10); pager.init(); pager.showPageNav('pager', 'pageStaff'); pager.showPage(1);";
			rootNode.appendChild(divPaging);
			rootNode.appendChild(script);
		}
	},

});

var staffModel = function(uId, username, lname, fname, email, addr, phone, role, ban) {
	this.uId = uId;
	this.username = username;
	this.lname = lname;
	this.fname = fname;
	this.email = email;
	this.addr = addr;
	this.phone = phone;
	this.role = role;
	this.ban = ban;
}

